
#import "ServerListViewController.h"
#import "ServerDetailsViewController.h"
#import "DownloadTorrentListViewController.h"

@implementation ServerListViewController

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// https mode
//
@synthesize useHttpsTracker													= _useHttpsTracker;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
		
	//
	// setup title, image
	//
	self.title																= NSLocalizedStringFromTable(@"Servers", @"ServerListViewController", nil);
	self.tabBarItem.image													= [UIImage imageNamed:@"tab_icon_server_list.png"];
	self.navigationItem.rightBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addServer:)] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{	
	self.torrentInfo														= nil;
	
	[super dealloc];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return [DataCenter sharedDataCenter].serverList.count;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	return self.torrentInfo.title;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= [tableView dequeueReusableCellWithIdentifier:@"d>>"];
	if(!theCell)
		theCell																= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d>>"] autorelease];
	
	id<TorrentClient> torrentClient											= [[DataCenter sharedDataCenter].serverList objectAtIndex:indexPath.row];
	theCell.textLabel.text													= [[torrentClient serverInfo] serverName];
	theCell.accessoryType													= UITableViewCellAccessoryDetailDisclosureButton;
	return theCell;
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	if(self.torrentInfo)
	{
		id addTorrentCallback = [[^void(id<TorrentClientRequest> torrentClientRequest)
		{
			[ActivityView removeView];
			NSError* error													= [torrentClientRequest error];
			NSString* title													= nil;
			if(error)
				title														= NSLocalizedStringFromTable(@"Add Torrent Failed", @"ServerListViewController", nil);
			else
				title														= NSLocalizedStringFromTable(@"Add Torrent Finished", @"ServerListViewController", nil);
			
			NSString* message												= error ? error.localizedDescription : NSLocalizedStringFromTable(@"Torrent has been passed to remote server", @"ServerListViewController", nil);
			[UIAlertView alertViewWithTitle:title message:message cancel:NSLocalizedString(@"OK", nil)];
		} copy] autorelease];
		
		NSString* title														= NSLocalizedString(@"Confirm", nil);
		id<TorrentClient> torrentClient										= [[DataCenter sharedDataCenter].serverList objectAtIndex:indexPath.row];
		NSString* message													= [NSString stringWithFormat:NSLocalizedStringFromTable(@"Are you sure want to add torrent to %@?", @"ServerListViewController", nil), [[torrentClient serverInfo] serverName]];
		id onDismiss = ^void(int buttonIndex)
		{
			[ActivityView showActivityView:NSLocalizedStringFromTable(@"Uploading Torrent", @"ServerListViewController", nil) networking:YES];
			[torrentClient addTorrent:[[NexusClient sharedClient] torrentDownloadAddress:self.torrentInfo passKey:[DataCenter sharedDataCenter].passKey httpsMode:self.useHttpsTracker] autoStart:YES callback:addTorrentCallback];
		};
		[tableView deselectRowAtIndexPath:indexPath animated:YES];
		[UIAlertView alertViewWithTitle:title message:message cancel:NSLocalizedString(@"Cancel", nil) others:[NSArray arrayWithObjects:NSLocalizedString(@"OK", nil), nil] onDismiss:onDismiss onCancel:nil];
	}
	else
	{
		DownloadTorrentListViewController* torrentListViewController		= [[[DownloadTorrentListViewController alloc] init] autorelease];
		torrentListViewController.torrentClient								= [[DataCenter sharedDataCenter].serverList objectAtIndex:indexPath.row];
		[self.navigationController pushViewController:torrentListViewController animated:YES];
	}
}

//
// select detail button
//
- (void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
{
	id<TorrentClient> torrentClient											= [[DataCenter sharedDataCenter].serverList objectAtIndex:indexPath.row];
	id editCallback = ^void(BOOL commit, ServerInfo* serverInfo)
	{
		if(commit && serverInfo)
		{
			[[DataCenter sharedDataCenter] updateServerAtIndex:indexPath.row fromServerInfo:serverInfo];
			[[DataCenter sharedDataCenter] saveServerList];
			[self.tableView reloadData];
		}
		
		[self.tabBarController dismissModalViewControllerAnimated:YES];
	};
	
	ServerDetailsViewController* serverDetailsViewController				= [[[ServerDetailsViewController alloc] initWithServerInfo:[torrentClient serverInfo]] autorelease];
	serverDetailsViewController.callback									= editCallback;
	UINavigationController* serverDetailsNavigationController				= [[[UINavigationController alloc] initWithRootViewController:serverDetailsViewController] autorelease];
	[self.tabBarController presentModalViewController:serverDetailsNavigationController animated:YES];
}

//
// can edit
//
- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath
{
	return YES;
}

//
// edit
//
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
	if(editingStyle == UITableViewCellEditingStyleDelete)
	{
		[[DataCenter sharedDataCenter] removeServerAtIndex:indexPath.row];
		[[DataCenter sharedDataCenter] saveServerList];
		[tableView reloadData];
	}
}

//
// add server
//
- (void)addServer:(id)sender
{
	id editCallback = ^void(BOOL commit, ServerInfo* serverInfo)
	{
		if(commit && serverInfo)
		{
			[[DataCenter sharedDataCenter] addServerFromServerInfo:serverInfo];
			[[DataCenter sharedDataCenter] saveServerList];
			[self.tableView reloadData];
		}
		
		[self.tabBarController dismissModalViewControllerAnimated:YES];
	};
	
	ServerDetailsViewController* serverDetailsViewController				= [[[ServerDetailsViewController alloc] initWithServerInfo:nil] autorelease];
	serverDetailsViewController.callback									= editCallback;
	UINavigationController* serverDetailsNavigationController				= [[[UINavigationController alloc] initWithRootViewController:serverDetailsViewController] autorelease];
	[self.tabBarController presentModalViewController:serverDetailsNavigationController animated:YES];
}

@end